package main.java.study.step02;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuwenxiu
 * @Description: instanceOf
 * @date 2022-7-13 19:49
 */
public class InstanceOfDemo {

    public static void main(String[] args) {
        List<GrandFather> lists = new ArrayList<>();
        lists.add(new GrandFather());
        lists.add(new Father());
        lists.add(new Son());
        lists.add(new Father());

        for (GrandFather list : lists) {
            if(list instanceof GrandFather){
                System.out.println(list + "是GrandFather类型或其子类");
            }else {
                System.out.println(list + "不是GrandFather类型或其子类");
            }
            if(list instanceof Father){
                System.out.println(list + "是Father类型或其子类");
            }else {
                System.out.println(list + "不是Father类型或其子类");
            }
            if(list instanceof Son){
                System.out.println(list + "是Son类型或其子类");
            }else {
                System.out.println(list + "不是Son类型或其子类");
            }
        }
    }
    
}

class GrandFather {
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }
}

class Father extends GrandFather{
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }
}

class Son extends Father{
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }
}
